package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.NoticeListDto;
import com.hualude.roadsign.dto.OrgDto4Notice;
import com.hualude.roadsign.repository.NoticeAddRepository;
import com.hualude.roadsign.repository.NoticeListRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.NoticeAddService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pjn on 2017/3/3.
 */
@Service
public class NoticeAddServiceImpl implements NoticeAddService {
    @Resource
    private NoticeAddRepository noticeAddRepository;
    @Resource
    private NoticeListRepository noticeListRepository;
    @Resource
    private UserRepository userRepository;

    @Transactional
    public String addNotice(Integer createUserId, String noticeTitle, String noticeContext, String orgIds) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        NoticeListDto noticeDto = new NoticeListDto();
        noticeDto.setCreateUserId(createUserId);
        noticeDto.setNoticeTitle(noticeTitle);
        noticeDto.setNoticeContent(noticeContext);
        noticeDto.setNoticeSendTime(df.format(new Date()));
        noticeDto.setNoticeType(0);
        Integer noticeId = noticeAddRepository.save(noticeDto.toBean()).getNoticeId();

        String[] orgIdArray = orgIds.split(",");
        for (int i = 0; i < orgIdArray.length; i++) {
            List<Integer> userIdDto = userRepository.getUserIds2NoticeAdd(Integer.parseInt(orgIdArray[i]));
            for (int j = 0; j < userIdDto.size(); j++) {
                NoticeListDto userNoticeDto = new NoticeListDto();
                userNoticeDto.setNoticeId(noticeId);
                userNoticeDto.setUserId(userIdDto.get(j));
                noticeListRepository.save(userNoticeDto.toUserNoticeBean());
            }
        }
        return "";
    }


    public DTResponseDto getNoticesended4DT(DTRequestDto dtRequestDto) throws Exception {
        List<NoticeListDto> dto4DTs = noticeAddRepository.getNoticesended4DT(dtRequestDto);
        Integer count = noticeAddRepository.getNoticesendedCount4DT(dtRequestDto.getSearch());
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
    }

    @Transactional
    public Integer deleteNotice(Integer userNoticeId) throws Exception {
        return noticeListRepository.deleteNotice(userNoticeId);
    }

    @Transactional
    public Integer deleteSelectNotice(String noticeIds) throws Exception {
        String[] noticeIdArray = noticeIds.split(",");
        for (int i = 0; i < noticeIdArray.length; i++) {
            noticeListRepository.deleteNoticeUser(Integer.parseInt(noticeIdArray[i]));
            noticeAddRepository.deleteNotice(Integer.parseInt(noticeIdArray[i]));
        }
        return 1;
    }

    public List<OrgDto4Notice> getSelectNoticeSendedOrg(Integer noticeId) throws Exception {
        return noticeAddRepository.getSelectNoticeSendedOrg(noticeId);
    }

    public List<OrgDto4Notice> getOrgInfo4Notice() throws Exception {
        return noticeAddRepository.getOrgInfo4Notice();
    }

    public List<NoticeListDto> getNoticeByUserId(Integer userId, Integer mCount, Integer mySize) throws Exception {
        return noticeListRepository.getNoticeByUserId4APP(userId, mCount, mySize);
    }

}
